package com.hdkj.eduservice.controller;

import com.hdkj.commonutils.JwtUtils;
import com.hdkj.commonutils.R;
import com.hdkj.eduservice.entity.timestamp;
import com.hdkj.eduservice.service.TimestampService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/eduservice/timestamp")
public class TimeStampController {

    @Autowired
    private TimestampService timestampService;


    @PostMapping("saveTs")
    public R saveTs(@RequestBody timestamp ts) {
        timestampService.save(ts);
        return R.ok();
    }

    @GetMapping("Tslist")
    public R tslist(HttpServletRequest request) {
        String uid = JwtUtils.getMemberIdByJwtToken(request);
        List<timestamp> timestampList = timestampService.list(new QueryWrapper<timestamp>().eq("userid",uid).orderByAsc("time"));
        return R.ok().data("tslist", timestampList);
    }


    @PostMapping("getinfo")
    public R getinfo(@RequestParam(name = "title") String title, @RequestParam(name = "uid") String uid) {
        timestamp timestamp = timestampService.getOne(new QueryWrapper<timestamp>().eq("userid",uid).eq("title", title));

        System.err.println(timestamp == null);

        if (timestamp == null) {
            return R.ok();
        }
        return R.error();
    }
}
